#!/usr/bin/env python
# Last Change: 2010-03-16 22:54:34

if __name__ == '__main__':
  import sys
  sys.path.append('../../')

from base import handler
from url import urlparse

from copy import copy
import os

class fshandler(handler):

  def name(self):
    return 'fs'

  def accept(self, url):
    if url.scheme == 'file' and url.fragment is None:
      return os.path.isdir(url.path)
    return False

  def list(self, url):
    subs = os.listdir(url.path)
    suburls = map(lambda x: copy(url) + x, subs)
    return suburls

  def format(self, url, cols):
    results = []
    for c in cols:
      if c == 'name':
        r = url.path.split('/')[-1]
        if not r:
          r = '/'
        results.append(r)
      else:
        r = ''
        results.append(r)
    return results

if __name__ == '__main__':
  h = fshandler()
  url = urlparse('file:///')
  print h.accept(url)
  print h.list(url)
  for u in h.list(url):
    print h.format(u, ['name'])
